﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Level1010 : MonoBehaviour
{
    public GameLevelItem gameLevelItem;
    public List<TextButton> buttonList = new List<TextButton>();
    public List<int> clickIndexList = new List<int>();
    public List<int> heratIndexList = new List<int>();
    public Color normalColor;
    // Start is called before the first frame update
    void Start()
    {
        InitBtnEvent();
        heratIndexList.Add(1);
        heratIndexList.Add(2);
        heratIndexList.Add(4);
        heratIndexList.Add(5);
        heratIndexList.Add(7);
        heratIndexList.Add(10);
        heratIndexList.Add(14);
        heratIndexList.Add(21);
        heratIndexList.Add(29);
        heratIndexList.Add(37);
        heratIndexList.Add(45);
        heratIndexList.Add(39);
        heratIndexList.Add(33);
        heratIndexList.Add(27);
        heratIndexList.Add(20);
        heratIndexList.Add(13);
    }

    private void InitBtnEvent()
    {
        clickIndexList.Clear();

        int count = buttonList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;

            buttonList[tempIndex].button.onClick.AddListener(() =>
            {
                SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
                if (clickIndexList.Contains(tempIndex))
                {
                    buttonList[tempIndex].button.image.color = normalColor;
                    clickIndexList.Remove(tempIndex);
                }
                else
                {
                    buttonList[tempIndex].button.image.color = Color.red;
                    clickIndexList.Add(tempIndex);
                }
                TriggerResult();
            });
        }
    }
    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.A))
        {
            int count = clickIndexList.Count;
            string debugStr = string.Empty;
            for (int i = 0; i < count; i++)
            {
                debugStr += clickIndexList[i].ToString() + "\n";
            }
            Debug.Log(debugStr);

        }

    }

    private void TriggerResult()
    {
        if (heratIndexList.Count == clickIndexList.Count)
        {
            int count = clickIndexList.Count;
            bool result = true;
            for (int i = 0; i < count; i++)
            {
                if (!heratIndexList.Contains(clickIndexList[i]))
                    result = false;
            }
            if (result)
                gameLevelItem.TriggerResult(true);
        }
    }

}
